package com.oracle.applicaiton.config;

import com.oracle.applicaiton.utils.RSAUtils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bug制造者
 * @ClassName JWTConfig.java
 * @createTime 2021年07月08日 14:52:00
 * @Description
 */
@Data
@Component
@ConfigurationProperties(prefix = "jwt")
public class JWTConfig {
    private String privateKey;

    public String createToken(Integer memberId,String loginname){
        Map<String,Object> map=new HashMap<>();
        map.put("memberId", memberId);
        map.put("loginname",loginname);
        //当前签发时间
        Date currentDate=new Date();
        //获取令牌过期时间
        Date expirtDate = DateUtils.addMinutes(currentDate, 300);
        String token = Jwts.builder().setClaims(map)
                .setIssuedAt(currentDate)
                .setExpiration(expirtDate)
                .setIssuer("BookStore-Auth").signWith(SignatureAlgorithm.RS256, RSAUtils.privateKeyStrToPrivate(privateKey)).compact();
        return token;
    }
}
